package com.wxyzz.cem.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxyzz.cem.enums.LoginRole;
import com.wxyzz.cem.exception.BackAdminServiceException;
import com.wxyzz.cem.mapper.BackAdminMapper;
import com.wxyzz.cem.pojo.BackAdmin;
import com.wxyzz.cem.util.EncryptUtil;
import com.wxyzz.cem.util.TokenUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BackAdminService extends ServiceImpl<BackAdminMapper, BackAdmin> {
    @Value("${cem.back-admin.register-enable: false}")
    private Boolean registerEnable;
    
    private String generateToken(BackAdmin backAdmin) {
        return TokenUtil.generate(backAdmin, LoginRole.BACK_ADMIN, backAdmin.getPassword());
    }
    
    public String encryptPassword(BackAdmin admin) {
        return EncryptUtil.encryptWithSha256(admin.getPassword(), admin.getAccount());
    }

    public boolean register(BackAdmin backAdmin) {
        if (!registerEnable) {
            throw new BackAdminServiceException("后台管理员注册功能已关闭！");
        }
        
        LambdaQueryWrapper<BackAdmin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BackAdmin::getAccount, backAdmin.getAccount());
        BackAdmin one = this.getOne(queryWrapper);
        if (one != null) {
            throw new BackAdminServiceException("账号已存在！");
        }
        
        String encryptPassword = EncryptUtil.encryptWithSha256(backAdmin.getPassword(), backAdmin.getAccount());
        backAdmin.setPassword(encryptPassword);
        
        return this.save(backAdmin);
    }

    public String login(BackAdmin backAdmin) {
        LambdaQueryWrapper<BackAdmin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BackAdmin::getAccount, backAdmin.getAccount());
        BackAdmin one = this.getOne(queryWrapper);
        if (one == null) {
            throw new BackAdminServiceException("账号不存在！");
        }
        String encryptPassword = this.encryptPassword(backAdmin);
        if (!one.getPassword().equals(encryptPassword)) {
            throw new BackAdminServiceException("密码错误！");
        }
        return generateToken(one);
    }

    public boolean addAdmin(BackAdmin backAdmin) {
        return this.save(backAdmin);
    }

    public boolean batchDelete(List<Integer> backAdminIds) {
        return this.removeByIds(backAdminIds);
    }

    public Page<BackAdmin> pageAdmin(Integer page, Integer size, String name, String phone, String account) {
        Page<BackAdmin> backAdminPage = new Page<>(page, size);
        LambdaQueryWrapper<BackAdmin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(name), BackAdmin::getName, name);
        queryWrapper.like(StrUtil.isNotBlank(phone), BackAdmin::getPhone, phone);
        queryWrapper.like(StrUtil.isNotBlank(account), BackAdmin::getAccount, account);
        return this.page(backAdminPage, queryWrapper);
    }
    
    public String updateAdmin(BackAdmin backAdmin) {
        this.updateById(backAdmin);
        return generateToken(backAdmin);
    }
}

